package com.test.juc;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 可重入锁
 * 显式锁
 * 加几次锁，释放几次，成对出现
 */
public class ReenterLockDemo2 {
    static Lock lock = new ReentrantLock();

    public static void m1(){
        new Thread(()->{
            lock.lock();
            try{
                System.out.println("---------外层");
                lock.lock();
                try{
                    System.out.println("------------中层");
                }finally {
                    lock.unlock();
                }
            }finally {
                lock.unlock();
            }

        },"t1").start();

        new Thread(()->{
            lock.lock();
            try{
                System.out.println("t2---------外层");
            }finally {
                lock.unlock();
            }

        },"t2").start();
    }

    public static void main(String[] args) {
        m1();
    }
}
